package tcpClient;
import info.clearthought.layout.TableLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ClientInterface2 extends javax.swing.JFrame implements ActionListener{
	private JTabbedPane TabbedPane;
	private JPanel sensorListPanel;
	private JPanel sensorControlPanel;
	private JLabel gpsLabel;
	private JTextArea sensorDataTA;
	private JTextArea listSensorTA;
	private JButton updateButton;
	private JTextField sensorColorLabel;
	private JTextField jTextField1;
	private JButton gpsOffButton;
	private JButton gpsOnButton;
	private JTextField gpsStatusTextField;
	private JButton historyLogButton;
	private JButton currentValueButton;
	private JButton offSensorButton;
	private JButton onSensorButton;
	private JLabel sensorIdLabel;
	private JButton showSensorButton;
	
	private Client client;

	/**
	* Auto-generated main method to display this JFrame
	*/
/*	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ClientInterface2 inst = new ClientInterface2();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}  */
	
	public ClientInterface2(Client client) {
		super();
		initGUI();
		this.client = client;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				TabbedPane = new JTabbedPane();
				getContentPane().add(TabbedPane, BorderLayout.CENTER);
				{
					sensorControlPanel = new JPanel();
					TabbedPane.addTab("Sensor List", null, sensorControlPanel, null);
					TableLayout sensorControlPanelLayout = new TableLayout(new double[][] {{95.0, 55.0, 135.0, 48.0, TableLayout.FILL}, {27.0, 28.0, 34.0, TableLayout.FILL, 114.0, TableLayout.FILL}});
					sensorControlPanel.setLayout(sensorControlPanelLayout);
					{
						showSensorButton = new JButton();
						sensorControlPanel.add(showSensorButton, "2, 1");
						showSensorButton.setText("Show sensors");
					}
					{
						listSensorTA = new JTextArea();
						sensorControlPanel.add(listSensorTA, "1, 3, 3, 4");
						listSensorTA.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
					}
				}
				{
					sensorListPanel = new JPanel();
					TabbedPane.addTab("Sensor Control Panel", null, sensorListPanel, null);
					TableLayout sensorListPanelLayout = new TableLayout(new double[][] {{7.0, 20.0, 59.0, 36.0, 12.0, 13.0, 35.0, 41.0, 39.0, 32.0, 46.0, 18.0, 46.0, TableLayout.FILL, TableLayout.FILL}, {10.0, 30.0, 7.0, 27.0, 10.0, 25.0, 24.0, 25.0, 17.0, 25.0, TableLayout.FILL, 7.0, 26.0, 8.0, TableLayout.FILL, 6.0, TableLayout.FILL, 7.0}});
					sensorListPanel.setLayout(sensorListPanelLayout);
					sensorListPanel.setPreferredSize(new java.awt.Dimension(475, 316));
					{
						sensorIdLabel = new JLabel();
						sensorListPanel.add(sensorIdLabel, "1, 1, 2, 1");
						sensorIdLabel.setText("Sensor ID:");
					}
					{
						onSensorButton = new JButton();
						sensorListPanel.add(onSensorButton, "2, 3");
						onSensorButton.setText("On");
					}
					{
						offSensorButton = new JButton();
						sensorListPanel.add(offSensorButton, "2, 5");
						offSensorButton.setText("Off");
					}
					{
						currentValueButton = new JButton();
						sensorListPanel.add(currentValueButton, "2, 7, 5, 7");
						currentValueButton.setText("Current Value");
					}
					{
						historyLogButton = new JButton();
						sensorListPanel.add(historyLogButton, "2, 9, 5, 9");
						historyLogButton.setText("History Log");
					}
					{
						gpsLabel = new JLabel();
						sensorListPanel.add(gpsLabel, "2, 12");
						gpsLabel.setText("GPS");
					}
					{
						gpsStatusTextField = new JTextField();
						sensorListPanel.add(gpsStatusTextField, "3, 12");
						gpsStatusTextField.setBackground(new java.awt.Color(236,233,216));
					}
					{
						gpsOnButton = new JButton();
						sensorListPanel.add(gpsOnButton, "2, 14");
						gpsOnButton.setText("On");
					}
					{
						gpsOffButton = new JButton();
						sensorListPanel.add(gpsOffButton, "2, 16");
						gpsOffButton.setText("Off");
					}
					{
						jTextField1 = new JTextField();
						sensorListPanel.add(jTextField1, "3, 1");
					}
					{
						sensorColorLabel = new JTextField();
						sensorListPanel.add(sensorColorLabel, "6, 1");
						sensorColorLabel.setBackground(new java.awt.Color(236,233,216));
					}
					{
						updateButton = new JButton();
						sensorListPanel.add(updateButton, "9, 14, 10, 15");
						updateButton.setText("Update");
					}
					{
						sensorDataTA = new JTextArea();
						sensorListPanel.add(sensorDataTA, "7, 4, 13, 11");
						sensorDataTA.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
					}
				}
				gpsOffButton.addActionListener(this);
				gpsOnButton.addActionListener(this);
				historyLogButton.addActionListener(this);
				currentValueButton.addActionListener(this);
				offSensorButton.addActionListener(this);
				onSensorButton.addActionListener(this);
				showSensorButton.addActionListener(this);
				updateButton.addActionListener(this);
				
			}
			pack();
			this.setSize(489, 387);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==gpsOffButton)
		{
			client.gpsOff();
			gpsStatusTextField.setBackground(new Color(255,0,0));
		}
		if(ae.getSource()==gpsOnButton)
		{
			client.gpsOn();
			gpsStatusTextField.setBackground(new Color(0,255,0));
		}
		if(ae.getSource()==historyLogButton)
		{
			ArrayList<String> list = new ArrayList<String>();
			System.out.println(Integer.parseInt(jTextField1.getText()));
			list = client.historylog(Integer.parseInt(jTextField1.getText()));
			if(list==null)
			{
				System.out.println("list==null");
			}
			System.out.println(list.size() + "   list.size");
			String list1 = null;
			for(int i = 0; i< list.size(); i ++)
			{
				list1 = list1 + list.get(i) + '\n';
			}
			sensorDataTA.setText(list1);
			
		}
		if(ae.getSource()==currentValueButton)
		{
			String curvalue;
			curvalue = client.getCurvalue(Integer.parseInt(jTextField1.getText()));
			sensorDataTA.setText(curvalue);
		}
		if(ae.getSource()==offSensorButton)
		{
			client.offSensor(Integer.parseInt(jTextField1.getText()));
			sensorColorLabel.setBackground(new Color(255,0,0));
		}
		if(ae.getSource()==onSensorButton)
		{
			client.onSensor(Integer.parseInt(jTextField1.getText()));
			sensorColorLabel.setBackground(new Color(0,255,0));
		}
		if(ae.getSource()==showSensorButton)
		{
			ArrayList<String> list = new ArrayList<String>();
			list = client.listsensor();
			String list1 = null;
			System.out.println(list.size());
			for(int i = 0; i < list.size(); i++)
			{
				list1 = list1 + list.get(i) + '\n';
			}
		//	listSensorsTextField.
			listSensorTA.setText(list1);  
		}
		if(ae.getSource()==updateButton)
		{
			client.update();
		}
	}

}
